﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using AzukiMap.MapSource;

namespace AzukiMapUI
{
	public partial class WMSLayerProperties : UserControl, IMapSourceProperties
	{

        WMSSource _wmsSource;

        public MapSource MapDataSource
        {
            get {
                SetAll();
                return _wmsSource; 
            }
        }

        public WMSSource WmsSource
        {
            get {
                SetAll();
                return _wmsSource; 
            }
            set { 
                _wmsSource = value;
                SetForm();
            }
        }

		public WMSLayerProperties()
		{
            _wmsSource = new WMSSource();
            Init();
		}

        public WMSLayerProperties(WMSSource wmsSource)
        {
            _wmsSource = wmsSource;
            Init();
        }

        void Init()
        {
			// Required to initialize variables
			InitializeComponent();

            SetForm();
            CheckAllFormData();
        }


        public bool CheckAllFormData()
        {
            bool isOk1 = CheckServerBase();
            bool isOk2 = CheckLayers();
            bool isOk3 = CheckSrid();
            bool isOk4 = CheckMaxScale();
            bool isOk5 = CheckMinScale();

            return (isOk1 && isOk2 && isOk3 && isOk4 && isOk5);
        }

        void SetForm()
        {
            FormInputControl.SetForm(TbServerBase, _wmsSource.RequestUri);
            FormInputControl.SetForm(CbWmsVersion, (int)_wmsSource.Version);
            FormInputControl.SetForm(TbLayers, _wmsSource.Layers);
            FormInputControl.SetForm(ChkBackground, _wmsSource.Transparent);
            FormInputControl.SetForm(CbFormat, (int)_wmsSource.Format);
            FormInputControl.SetForm(TbSrid, _wmsSource.Srid);
            FormInputControl.SetForm(CbLatLon, (int)_wmsSource.BBoxLatLonOrder);
            FormInputControl.SetForm(TbMaxScale, _wmsSource.MaxScale);
            FormInputControl.SetForm(TbMinScale, _wmsSource.MinScale);
        }

        bool CheckServerBase()
        {
            return FormInputControl.CheckUri(TbServerBase.Text, IconErrorServerBase);
        }

        bool CheckLayers()
        {
            return FormInputControl.HasData(TbLayers.Text, IconErrorLayers);
        }

        bool CheckSrid()
        {
            return FormInputControl.CheckNaturalNumber(TbSrid.Text, IconErrorSrid);
        }

        bool CheckMaxScale()
        {
            return FormInputControl.CheckPositiveDouble(TbMaxScale.Text, IconErrorMaxScale);
        }

        bool CheckMinScale()
        {
            return FormInputControl.CheckPositiveDouble(TbMinScale.Text, IconErrorMinScale, true);
        }

        void SetAll()
        {
            SetServerBase();
            SetWmsVersion();
            SetLayers();
            SetOpacity();
            SetFormat();
            SetSrid();
            SetLatLon();
            SetMaxScale();
            SetMinScale();
        }

        void SetServerBase()
        {
            if (CheckServerBase())
                _wmsSource.RequestUri = TbServerBase.Text;
        }

        void SetWmsVersion()
        {
            string versionStr = FormInputControl.GetSelectedTextFromCombo(CbWmsVersion);
            _wmsSource.Version = _wmsSource.GetWMSVersionFromString(versionStr);
        }

        void SetLayers()
        {
            if (CheckLayers())
                FormInputControl.SetListDataFromForm(_wmsSource.Layers, TbLayers.Text);
        }

        void SetOpacity()
        {
            _wmsSource.Transparent = (bool)ChkBackground.IsChecked;
        }

        void SetFormat()
        {
            string formatStr = FormInputControl.GetSelectedTextFromCombo(CbFormat);
            _wmsSource.Format = _wmsSource.WMSImageFormatFromString(formatStr);
        }

        void SetSrid()
        {
            if (CheckSrid())
                _wmsSource.Srid = int.Parse(TbSrid.Text);
        }

        void SetLatLon()
        {
            string latLonStr = FormInputControl.GetSelectedTextFromCombo(CbLatLon);
            _wmsSource.BBoxLatLonOrder = 
                (latLonStr == "Lon - Lat") ? LatLonOrder.LON_LAT : LatLonOrder.LAT_LON;
        }

        void SetMaxScale()
        {
            if (CheckMaxScale())
                _wmsSource.MaxScale = double.Parse(TbMaxScale.Text);
        }

        void SetMinScale()
        {
            if (CheckMinScale())
                _wmsSource.MinScale = double.Parse(TbMinScale.Text);
        }

        void TbServerBase_LostFocus(object sender, RoutedEventArgs e)
        {
            CheckServerBase();
        }

        void CbWmsVersion_LostFocus(object sender, RoutedEventArgs e)
        {
        }

        void TbLayers_LostFocus(object sender, RoutedEventArgs e)
        {
            CheckLayers();
        }

        void ChkBackground_LostFocus(object sender, RoutedEventArgs e)
        {
        }

        void CbFormat_LostFocus(object sender, RoutedEventArgs e)
        {
        }

        void TbSrid_LostFocus(object sender, RoutedEventArgs e)
        {
            CheckSrid();
        }

        void CbLatLon_LostFocus(object sender, RoutedEventArgs e)
        {
        }

        void TbMaxScale_LostFocus(object sender, RoutedEventArgs e)
        {
            CheckMaxScale();
        }

        void TbMinScale_LostFocus(object sender, RoutedEventArgs e)
        {
            CheckMinScale();
        }

    }
}